package com.xuecheng.test.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {
    public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
    public static final String QUEUE_INFORM_SMS = "queue_inform_sms";
    public static final String EXCHANGE_TOPIC_INFORM="exchange_topic_inform";
    public static final String EXCHANGE_TOPIC_EMAIL="inform.#.email.#";
    public static final String EXCHANGE_TOPIC_SMS="inform.#.sms.#";


    //声明交换机
    @Bean(EXCHANGE_TOPIC_INFORM)
    public Exchange EXCHANGE_TOPIC_INFORM(){
        //durable(true) 交换机要持久化 mq重启之后交换机还在
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPIC_INFORM).durable(true).build();
    }

    //声明队列email
    @Bean(QUEUE_INFORM_EMAIL)
    public Queue QUEUE_INFORM_EMAIL(){
        return new Queue(QUEUE_INFORM_EMAIL);
    }
    //声明队列sms
    @Bean(QUEUE_INFORM_SMS)
    public Queue QUEUE_INFORM_SMS(){
        return new Queue(QUEUE_INFORM_SMS);
    }

    //队列交换机绑定 email队列 指定routeKey
    @Bean
    public Binding BIND_QUEUE_EXCHANGE_EMAIL(@Qualifier(QUEUE_INFORM_EMAIL) Queue queue,
                                             @Qualifier(EXCHANGE_TOPIC_INFORM) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(EXCHANGE_TOPIC_EMAIL).noargs();
    }

    //队列交换机绑定 sms队列 指定routeKey
    @Bean
    public Binding BIND_QUEUE_EXCHANGE_SMS(@Qualifier(QUEUE_INFORM_SMS) Queue queue,
                                             @Qualifier(EXCHANGE_TOPIC_INFORM) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(EXCHANGE_TOPIC_SMS).noargs();
    }

}
